import UIKit

class Switch: UIViewController {
    
    // MARK: - 控件属性
    private var switchControl = UISwitch()
    private var statusLabel = UILabel()
    private var toggleButton = UIButton(type: .system)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        setupUI()
        setupConstraints()
    }
    
    // MARK: - UI 设置
    private func setupUI() {
        // 创建 UISwitch
        switchControl = UISwitch()
        switchControl.translatesAutoresizingMaskIntoConstraints = false
        switchControl.onTintColor = .systemBlue // 开启时的轨道颜色
        switchControl.tintColor = .systemGray // 未开启时的轨道颜色
        switchControl.thumbTintColor = .white // 滑块颜色
        switchControl.addTarget(self, action: #selector(switchValueChanged(_:)), for: .valueChanged)
        view.addSubview(switchControl)
        
        // 创建状态标签
        statusLabel = UILabel()
        statusLabel.translatesAutoresizingMaskIntoConstraints = false
        statusLabel.font = UIFont.systemFont(ofSize: 16)
        statusLabel.text = "开关状态：关闭"
        statusLabel.numberOfLines = 0 // 允许多行（可选）
        view.addSubview(statusLabel)
        
        // 创建手动切换按钮
        toggleButton = UIButton(type: .system)
        toggleButton.translatesAutoresizingMaskIntoConstraints = false
        toggleButton.setTitle("手动切换", for: .normal)
        toggleButton.backgroundColor = .systemBlue
        toggleButton.tintColor = .white
        toggleButton.layer.cornerRadius = 8
        toggleButton.addTarget(self, action: #selector(manualToggleSwitch), for: .touchUpInside)
        view.addSubview(toggleButton)
    }
    
    // MARK: - 布局约束
    private func setupConstraints() {
        let margin: CGFloat = 20
        NSLayoutConstraint.activate([
            // Switch 约束（安全区域）
            switchControl.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
            switchControl.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: margin * 2),
            switchControl.heightAnchor.constraint(equalToConstant: 31), // UISwitch 标准高度
            
            // 状态标签约束
            statusLabel.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
            statusLabel.topAnchor.constraint(equalTo: switchControl.bottomAnchor, constant: 16), // 间距 16pt
            statusLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: margin),
            statusLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -margin),
            
            // 按钮约束
            toggleButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: margin),
            toggleButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -margin),
            toggleButton.heightAnchor.constraint(equalToConstant: 50),
            toggleButton.topAnchor.constraint(equalTo: statusLabel.bottomAnchor, constant: 24), // 间距 24pt
        ])
    }
    
    // MARK: - 交互逻辑
    @objc private func switchValueChanged(_ sender: UISwitch) {
        //  \(...)：在 Swift 里，这是字符串插值的语法，能够把表达式的结果嵌入到字符串当中。
        statusLabel.text = "开关状态：\(sender.isOn ? "开启" : "关闭")"
        updateButtonColor()
    }
    
    @objc private func manualToggleSwitch() {
        // 手动切换开关状态
        //toggle()：这是 Swift 标准库为布尔类型提供的一个方法，调用该方法能够使布尔值在 true 和 false 之间进行切换，也就是让 true 变为 false，或者让 false 变为 true。
        switchControl.isOn.toggle()
        // 触发状态更新
        switchValueChanged(switchControl)
    }
    
    private func updateButtonColor() {
        // 根据开关状态改变按钮背景色
        toggleButton.backgroundColor = switchControl.isOn ? .systemGreen : .systemBlue
    }
}
